# -*- coding: utf-8 -*-
"""
场景2：模拟N个用户并发注册账号，共有5个账号，要求注册账号不重复，但数据可循环注册
"""

from locust import TaskSet, task, HttpUser, constant
import queue
import os


class UserTask(TaskSet):
    @task
    def test_register(self):
        try:
            print(f'share_data:{self.user.share_data.queue}')
            data = self.user.share_data.get()
        except queue.Empty:
            print('account data run out, test ended.')
            exit(0)
        print(f'register with user: {data["username"]}, pwd: {data["password"]}')
        self.user.share_data.put_nowait(data)  # 将消费掉数据又加回到队列中


class WebsiteUser(HttpUser):
    host = 'http://www.baidu.com'
    wait_time = constant(1)
    task_set = task(UserTask)
    share_data = queue.Queue()
    for index in range(5):
        data = {"username": f"test{index}", "password": f"pwd{index}"}
        share_data.put_nowait(data)


if __name__ == '__main__':
    current_file_path = os.path.abspath(__file__)
    os.system(f"locust -f {current_file_path}")
